package com.itwlj.mapper;

import com.github.pagehelper.Page;
import com.itwlj.annotation.AutoFill;
import com.itwlj.dto.UserPageQueryDTO;
import com.itwlj.entity.User;
import com.itwlj.enumeration.OperationType;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Map;

@Mapper
public interface UserMapper {
    /**
     * 根据账号和类型查询用户
     *
     * @param account
     * @param type
     * @return
     */
    @Select("select * from user where account = #{account} and type = #{type}")
    User getByAccount(String account, Integer type);

    /**
     * 新增用户
     *
     * @param user
     */
    @AutoFill(OperationType.INSERT)
    void insert(User user);

    /**
     * 用户分页查询
     *
     * @param userPageQueryDTO
     * @return
     */
    Page<User> pageQuery(UserPageQueryDTO userPageQueryDTO);

    /**
     * 根据id修改用户
     *
     * @param user
     */
    @AutoFill(OperationType.UPDATE)
    void update(User user);

    /**
     * 根据id和密码查询用户
     *
     * @param id
     * @param password
     * @return
     */
    @Select("select count(*) > 0 from user where id = #{id} and password = md5(#{password})")
    Boolean getByIdAndPassword(Long id, String password);

    /**
     * 根据用户id修改创建人和修改人
     *
     * @param id
     */
    @Update("update user SET create_user = #{id}, update_user = #{id} WHERE id = #{id}")
    void updateCreateUserAndUpdateUser(Long id);

    /**
     * 根据用户名和手机号查询用户
     *
     * @param account
     * @param phone
     * @return
     */
    @Select("select count(*) > 0 from user where account = #{account} and phone = #{phone}")
    Boolean getByAccountAndPhone(String account, String phone);

    /**
     * 根据账号修改密码
     *
     * @param account
     * @param password
     */
    @Update("update user set password = md5(#{password}) where account = #{account}")
    void updateByAccount(String account, String password);

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @Select("select * from user where id = #{id}")
    User getById(Long id);

    /**
     * 根据动态条件统计用户数量
     *
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    /**
     * 根据id扣减余额
     *
     * @param id
     * @param balance
     */
    @Update("update user set balance = #{balance} where id = #{id}")
    void deductBalance(Long id, BigDecimal balance);
}
